package com.yeyks.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;

public class Utility {

    public static String setExtraInfo(String extraInfo, String key, Object value) {
        try {
            JSONObject jsonObject = (JSONObject) JSON.parse(extraInfo);
            jsonObject.put(key, value);
            return jsonObject.toJSONString();
        } catch (Throwable e) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(key, value);
            return jsonObject.toJSONString();
        }
    }

    public static JSONObject toJsonObject(String extraInfo) {
        try {
            JSONObject jo =  (JSONObject) JSON.parse(extraInfo);
            if (jo == null) {
                return new JSONObject();
            } else {
                return jo;
            }
        } catch (Throwable e) {
            return new JSONObject();
        }
    }

    private static SecureRandom random = new SecureRandom();
    public static String randomString(int length) {
        String str = new BigInteger(130, random).toString(32);
        return str.substring(0, length);
    }

    public static String getOrderNo() {
        return new Date().getTime() + randomString(7);
    }

    public static String getRemoteIP(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }

    public static String randomPwd() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            sb.append(random.nextInt(10));
        }
        return "yyks" + sb.toString();
    }

    public static String randomName() {
        return "User_" + UUID.randomUUID().toString().substring(0, 7);
    }

    public static String invateCode() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);
    }

    /**
     * 获得n位随机验证码
     *
     * @return
     */
    public static String getRandom(Integer length) {
        String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(str.charAt(random.nextInt(str.length())));
        }
        return sb.toString();
    }

    /**
     * 获得n位随机验证码
     *
     * @return
     */
    public static String getFigureRandom(Integer length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(str.charAt(random.nextInt(str.length())));
        }
        return sb.toString();
    }

    /**
     * 获取两个数之间的随机数
     *
     * @param minCount
     * @param maxCount
     * @return
     */
    public static int getRandom(Integer minCount, Integer maxCount) {
        return new Random().nextInt(maxCount - minCount) + minCount;
    }

//    public static void main(String[] args) {
//        for (int i = 0; i < 1000; i++) {
//            System.out.println(Utility.getRandom(4));
//        }
//    }
}
